package com.lt.bookkeeping.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.it.bookkeeping.greendao.Dao;
import com.lt.bookkeeping.R;
import com.lt.bookkeeping.dataSave.BookKeepData;
import com.lt.bookkeeping.recycleView.BookKeepRecycleViewAdapter;
import com.lt.bookkeeping.recycleView.FootViewHolder;
import com.lt.bookkeeping.recycleView.ILoadMore;
import com.lt.bookkeeping.recycleView.ITop;
import com.lt.bookkeeping.recycleView.LoadMoreListen;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuTong
 */
public class FragmentDataStatistic extends BaseFragment{

    private RecyclerView recyclerView;
    private BookKeepRecycleViewAdapter bookKeepRecycleViewAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private int currentPage = 0;
    private final int REQUEST_COUNTS = 10;
    private List<BookKeepData> dataList = new ArrayList<>();
    private int allDataCounts;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         View view =inflater.inflate(R.layout.fragment_data_statistic,container,false);
         recyclerView = view.findViewById(R.id.data_recycle_view);
         swipeRefreshLayout = view.findViewById(R.id.data_swipe_refresh_layout);
         initEvent();
         return view;
    }

    private void initEvent() {

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        bookKeepRecycleViewAdapter = new BookKeepRecycleViewAdapter(dataList);
        recyclerView.setAdapter(bookKeepRecycleViewAdapter);
        allDataCounts = Dao.getInstance(daoSession).size();
        loadMoreData(currentPage,REQUEST_COUNTS,allDataCounts,true);


        ITop iTop = new ITop() {
            @Override
            public void top(int top) {
                swipeRefreshLayout.setEnabled(top >= 0);
            }
        };

        ILoadMore iLoadMore = new ILoadMore() {
            @Override
            public void loadMore() {
                if(bookKeepRecycleViewAdapter.getItemCount()-1 < allDataCounts){
                    currentPage++;
                    loadMoreData(currentPage,REQUEST_COUNTS,allDataCounts,false);
                }else {
                    bookKeepRecycleViewAdapter.setFootStatus(FootViewHolder.NO_DATA);
                }
            }
        };

        recyclerView.addOnScrollListener(new LoadMoreListen(iLoadMore,iTop,allDataCounts));

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getActivity().getWindow().getDecorView().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        BookKeepData bookKeepData = new BookKeepData();
                        bookKeepData.setMoney(12.32);
                        bookKeepData.setEvent("买书");
                        bookKeepData.setBookKeepTime(System.currentTimeMillis());
                        List<BookKeepData> newData = new ArrayList<>();
                        newData.add(bookKeepData);
                        bookKeepRecycleViewAdapter.upDate(newData);
                        swipeRefreshLayout.setRefreshing(false);
                        recyclerView.scrollToPosition(0);
                    }
                },1500);
            }
        });
    }


    /****
     *
     * @param currentSize
     * @param requestCounts
     * @param allSize
     * @param isTop 是否滑倒顶部
     */
    private void loadMoreData(int currentSize, int requestCounts, int allSize,boolean isTop){
        if(bookKeepRecycleViewAdapter.getItemCount()-1 < allSize){
            bookKeepRecycleViewAdapter.setFootStatus(FootViewHolder.LOADING);
            Dao.getInstance(daoSession).query(currentSize, requestCounts, new Dao.requestResult() {
                @Override
                public void success(List<BookKeepData> bookKeepDataList, int counts) {
                    bookKeepRecycleViewAdapter.upDate(bookKeepDataList);
                    if(bookKeepRecycleViewAdapter.getItemCount() -1 >= counts){
                        bookKeepRecycleViewAdapter.setFootStatus(FootViewHolder.NO_DATA);
                        if(isTop){
                            recyclerView.scrollToPosition(0);
                        }
                    }
                }
                @Override
                public void fail() {

                }
            });

        }
    }

    @Override
    public void onResume() {
        super.onResume();
//        initEvent();
    }
}
